Imports System.Windows.Forms
Imports WindowsLive.Writer.Api

Public Class PropertiesPanelEditor
    Inherits SmartContentEditor

#Region "InitializeComponent"

    Private WithEvents NumericUpDownLineValue As System.Windows.Forms.NumericUpDown
    Private WithEvents CheckShowColumns As System.Windows.Forms.CheckBox
    Private WithEvents CheckFirstLine As System.Windows.Forms.CheckBox
    Private WithEvents CheckCollapse As System.Windows.Forms.CheckBox
    Private WithEvents CheckNoControls As System.Windows.Forms.CheckBox
    Private WithEvents CheckNoGutter As System.Windows.Forms.CheckBox
    Private WithEvents ListLanguage As System.Windows.Forms.ComboBox
    Private WithEvents OptionToolTip As System.Windows.Forms.ToolTip
    Private components As System.ComponentModel.IContainer
    Private WithEvents CheckEmbedStyle As System.Windows.Forms.CheckBox
    Private WithEvents ButtonEdit As System.Windows.Forms.Button


    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim GroupOption As System.Windows.Forms.GroupBox
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(PropertiesPanelEditor))
        Me.NumericUpDownLineValue = New System.Windows.Forms.NumericUpDown
        Me.CheckShowColumns = New System.Windows.Forms.CheckBox
        Me.CheckFirstLine = New System.Windows.Forms.CheckBox
        Me.CheckCollapse = New System.Windows.Forms.CheckBox
        Me.CheckNoControls = New System.Windows.Forms.CheckBox
        Me.CheckNoGutter = New System.Windows.Forms.CheckBox
        Me.ButtonEdit = New System.Windows.Forms.Button
        Me.ListLanguage = New System.Windows.Forms.ComboBox
        Me.OptionToolTip = New System.Windows.Forms.ToolTip(Me.components)
        Me.CheckEmbedStyle = New System.Windows.Forms.CheckBox
        GroupOption = New System.Windows.Forms.GroupBox
        GroupOption.SuspendLayout()
        CType(Me.NumericUpDownLineValue, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'GroupOption
        '
        GroupOption.Controls.Add(Me.NumericUpDownLineValue)
        GroupOption.Controls.Add(Me.CheckShowColumns)
        GroupOption.Controls.Add(Me.CheckFirstLine)
        GroupOption.Controls.Add(Me.CheckCollapse)
        GroupOption.Controls.Add(Me.CheckNoControls)
        GroupOption.Controls.Add(Me.CheckNoGutter)
        GroupOption.FlatStyle = System.Windows.Forms.FlatStyle.System
        resources.ApplyResources(GroupOption, "GroupOption")
        GroupOption.Name = "GroupOption"
        GroupOption.TabStop = False
        '
        'NumericUpDownLineValue
        '
        resources.ApplyResources(Me.NumericUpDownLineValue, "NumericUpDownLineValue")
        Me.NumericUpDownLineValue.Maximum = New Decimal(New Integer() {2147483647, 0, 0, 0})
        Me.NumericUpDownLineValue.Minimum = New Decimal(New Integer() {1, 0, 0, 0})
        Me.NumericUpDownLineValue.Name = "NumericUpDownLineValue"
        Me.NumericUpDownLineValue.Value = New Decimal(New Integer() {1, 0, 0, 0})
        '
        'CheckShowColumns
        '
        resources.ApplyResources(Me.CheckShowColumns, "CheckShowColumns")
        Me.CheckShowColumns.Name = "CheckShowColumns"
        Me.CheckShowColumns.UseVisualStyleBackColor = True
        '
        'CheckFirstLine
        '
        resources.ApplyResources(Me.CheckFirstLine, "CheckFirstLine")
        Me.CheckFirstLine.Name = "CheckFirstLine"
        Me.CheckFirstLine.UseVisualStyleBackColor = True
        '
        'CheckCollapse
        '
        resources.ApplyResources(Me.CheckCollapse, "CheckCollapse")
        Me.CheckCollapse.Name = "CheckCollapse"
        Me.CheckCollapse.UseVisualStyleBackColor = True
        '
        'CheckNoControls
        '
        resources.ApplyResources(Me.CheckNoControls, "CheckNoControls")
        Me.CheckNoControls.Name = "CheckNoControls"
        Me.CheckNoControls.UseVisualStyleBackColor = True
        '
        'CheckNoGutter
        '
        resources.ApplyResources(Me.CheckNoGutter, "CheckNoGutter")
        Me.CheckNoGutter.Name = "CheckNoGutter"
        Me.CheckNoGutter.UseVisualStyleBackColor = True
        '
        'ButtonEdit
        '
        resources.ApplyResources(Me.ButtonEdit, "ButtonEdit")
        Me.ButtonEdit.Name = "ButtonEdit"
        Me.OptionToolTip.SetToolTip(Me.ButtonEdit, resources.GetString("ButtonEdit.ToolTip"))
        Me.ButtonEdit.UseVisualStyleBackColor = True
        '
        'ListLanguage
        '
        Me.ListLanguage.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        resources.ApplyResources(Me.ListLanguage, "ListLanguage")
        Me.ListLanguage.FormattingEnabled = True
        Me.ListLanguage.Name = "ListLanguage"
        Me.OptionToolTip.SetToolTip(Me.ListLanguage, resources.GetString("ListLanguage.ToolTip"))
        '
        'CheckEmbedStyle
        '
        resources.ApplyResources(Me.CheckEmbedStyle, "CheckEmbedStyle")
        Me.CheckEmbedStyle.Name = "CheckEmbedStyle"
        Me.CheckEmbedStyle.UseVisualStyleBackColor = True
        '
        'PropertiesPanelEditor
        '
        Me.Controls.Add(Me.CheckEmbedStyle)
        Me.Controls.Add(Me.ListLanguage)
        Me.Controls.Add(GroupOption)
        Me.Controls.Add(Me.ButtonEdit)
        Me.Name = "PropertiesPanelEditor"
        GroupOption.ResumeLayout(False)
        CType(Me.NumericUpDownLineValue, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
#End Region

    Private _dialogOwner As IWin32Window

    Public Property DialogOwner() As IWin32Window
        Get
            Return _dialogOwner
        End Get
        Set(ByVal value As IWin32Window)
            _dialogOwner = value
        End Set
    End Property

    Private _pluginOptions As IProperties


    Public Property PluginOptions() As IProperties
        Get
            Return _pluginOptions
        End Get
        Set(ByVal value As IProperties)
            _pluginOptions = value
        End Set
    End Property

    Public Sub New()
        InitializeComponent()
    End Sub



    Private Sub PropertiesPanelEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ListLanguage.Items.AddRange(PluginSetting.Languages.ToArray)
        Try
            If SelectedContent IsNot Nothing Then
                ListLanguage.SelectedItem = (From l In PluginSetting.Languages Where SelectedContent.Properties.GetLanguage = l.ClassName).Single
            End If
        Catch ex As InvalidOperationException
            SelectedContent.Properties.SetLanguage(PluginSetting.Languages.First.ClassName)
            ListLanguage.SelectedIndex = 0
        End Try

    End Sub


    Private Sub PropertiesPanelEditor_SelectedContentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SelectedContentChanged

        ListLanguage.SelectedItem = (From l In PluginSetting.Languages Where SelectedContent.Properties.GetLanguage = l.ClassName).Single

        With SelectedContent.Properties

            CheckNoGutter.Checked = .GetNoGutter
            CheckNoControls.Checked = .GetNoControls
            CheckCollapse.Checked = .GetCollapse
            CheckFirstLine.Checked = .GetFirstLine
            CheckShowColumns.Checked = .GetShowColumns
            NumericUpDownLineValue.Value = .GetLineValue

        End With

    End Sub

    Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles CheckNoGutter.CheckedChanged, CheckNoControls.CheckedChanged, CheckCollapse.CheckedChanged, CheckFirstLine.CheckedChanged, CheckShowColumns.CheckedChanged
        If SelectedContent Is Nothing Then
            Exit Sub
        End If

        If sender Is CheckNoGutter Then
            SelectedContent.Properties.SetNoGutter(CheckNoGutter.Checked)

        ElseIf sender Is CheckNoControls Then
            SelectedContent.Properties.SetNoControls(CheckNoControls.Checked)

        ElseIf sender Is CheckCollapse Then
            SelectedContent.Properties.SetCollapse(CheckCollapse.Checked)

        ElseIf sender Is CheckFirstLine Then
            SelectedContent.Properties.SetFirstLine(CheckFirstLine.Checked)

        ElseIf sender Is CheckShowColumns Then
            SelectedContent.Properties.SetShowColumns(CheckShowColumns.Checked)

        End If

        OnContentEdited()
    End Sub

    Private Sub CheckFirstLine_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckFirstLine.CheckedChanged
        If SelectedContent Is Nothing Then
            Exit Sub
        End If

        NumericUpDownLineValue.Enabled = CheckFirstLine.Checked
    End Sub

    Private Sub NumericUpDownLineValue_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownLineValue.ValueChanged
        If SelectedContent Is Nothing Then
            Exit Sub
        End If

        SelectedContent.Properties.SetLineValue(NumericUpDownLineValue.Value)
        OnContentEdited()
    End Sub

    Private Sub ListLanguage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListLanguage.SelectedIndexChanged
        If SelectedContent Is Nothing Then
            Exit Sub
        End If

        SelectedContent.Properties.SetLanguage(DirectCast(ListLanguage.SelectedItem, PluginSetting.LanguageItem).ClassName)
        OnContentEdited()
    End Sub

    Private Sub ButtonEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEdit.Click

        Dim action = PluginOptions.GetEditedAction

        Dim code As String
        If (action = EditorActions.PasteAndFix OrElse _
            action = EditorActions.PasteText) AndAlso _
           Clipboard.ContainsText Then
            code = Clipboard.GetText
        Else
            code = SelectedContent.Properties.GetCode
        End If

        Dim frm = New EditForm(SelectedContent.Properties.GetLanguage, code)
        frm.SetTabSize(PluginOptions.GetTabSize)
        If action = EditorActions.SelectAll Then
            frm.SelectAll()
        ElseIf action = EditorActions.PasteAndFix Then
            frm.FixIndent()
        End If

        If frm.ShowDialog(DialogOwner) = DialogResult.OK Then

            SelectedContent.Properties.SetLanguage(frm.Language)
            SelectedContent.Properties.SetCode(frm.Code)

            ' Setting drop down list of language
            ListLanguage.SelectedItem = (From l In PluginSetting.Languages Where SelectedContent.Properties.GetLanguage = l.ClassName).Single

            OnContentEdited()
        End If
        frm.Dispose()

    End Sub

End Class
